//触摸屏获取触摸坐标
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <linux/input.h>
#include<sys/mman.h>

struct input_event ts_buf;
int x,y;                //存储触点位置信息
int ts_fd;              //触摸文件
int color = 0xFFFF00;   //进度条颜色信息
int *p = NULL;          //进度条屏幕信息指针
int *lcd_map = NULL;

//进度条
void jindu(){

    int lcd_fd = open("/dev/fb0",O_RDWR);
    if (-1 == lcd_fd)
    {
        perror("open() lcd failed\n");
        return -1;
    }
    else
    {
        printf("open() lcd success\n");
    }

    p = mmap(NULL,800*480*4, PROT_READ|PROT_WRITE, MAP_SHARED,lcd_fd,0);

    if (NULL == p)
    {
        perror("mmap() failed\n");
        return 0;
    }


    for (size_t i = 80; i < 200; i++)
	{
		for(int r = 380; r <= 400;r++){
		    *(p+i+r*800) = color;
	    }
	}
    sleep(1);
    for (size_t i = 200; i < 400; i++)
	{
		for(int r = 380; r <= 400;r++){
		    *(p+i+r*800) = color;
	    }
	}
    sleep(1);
    for (size_t i = 400; i < 700; i++)
	{
		for(int r = 380; r <= 400;r++){
		    *(p+i+r*800) = color;
	    }
	}
    
    close(lcd_fd);
}

//显示图片
int show_bmp(char *picname, int x0, int y0 )
{
	//打开lcd
	int lcd_fd;
	lcd_fd = open("/dev/fb0", O_RDWR);
	if(-1 == lcd_fd)
	{
		perror("open lcd failed!\n");
		//return -1;
	}
	lcd_map = mmap(NULL, 800*480*4, PROT_READ|PROT_WRITE, MAP_SHARED, lcd_fd, 0);
	if(NULL == lcd_map)
	{
		perror("mmap failed!\n");
	}
	//打开图片
	int bmp_fd;
	bmp_fd = open(picname, O_RDONLY);
	if(-1 == bmp_fd)
	{
		perror("open xiaotu failed!\n");
		return -1;
	}
	
	//利用头54个字节，获得图片的宽和高
	char head[54];
	read(bmp_fd, head, 54);
	int w,h;
	w = *((int *)&head[18]);
	h = *((int *)&head[22]);
	printf("w=%d,h=%d\n",w,h);
	
	int nab,len;
	nab = (4-w*3%4)%4;//需要补齐的字节数
	len = w*3+nab;//补齐后，图片一行的像素字节长度
	
	//偏移54个字节，去掉头信息
	lseek(bmp_fd, 54, SEEK_SET);
	
	//定义图片像素数据缓冲区
	char bmp_buf[len*h*3];
	read(bmp_fd, bmp_buf, sizeof(bmp_buf));
	
	char bmp_bufn[w*h*3];
	for(int j=0;j<h;j++)
	{
		memcpy(&bmp_bufn[j*w*3], &bmp_buf[j*len], w*3);
	}
	//讲24位bmp图片像素转换为32位
	int buf32[w*h];
	int i;
	for(i=0; i<w*h; i++)
	{
		buf32[i] = bmp_bufn[0+i*3]<<0 | bmp_bufn[1+i*3]<<8 | bmp_bufn[2+i*3]<<16 | 0x00<<24;
	}
	
	//翻转图片,将颜色数据写入lcd
	int x,y;
	for(y=0;y<h;y++)
	{
		for(x=0;x<w;x++)
		{
			lcd_map[800*(y0+y)+x0+x] = buf32[w*(h-1-y)+x];
		}
	}
	//关闭bmp
	close(bmp_fd);
}


//打开触摸屏的驱动
int ts_open(void)
{	
	ts_fd = open("/dev/input/event0", O_RDWR);
	if(-1 == ts_fd)
	{
		perror("open ts failed!\n");
		return -1;
	}
}

int ts_get(int *x, int *y)
{
	//读取触摸屏的数据
	bzero(&ts_buf, sizeof(ts_buf));
	while(1)
	{
		read(ts_fd, &ts_buf, sizeof(ts_buf));
		if(ts_buf.type == EV_ABS && ts_buf.code == ABS_X)
		{
			*x = ts_buf.value*800/1024;//1024
		}
		if(ts_buf.type == EV_ABS && ts_buf.code == ABS_Y)
		{
			*y = ts_buf.value*480/600;//600
		}
		if(ts_buf.type == EV_KEY && ts_buf.code == BTN_TOUCH && ts_buf.value == 0)
		{
			break;
		}
	}	
}

int main()
{
    int jindu_flag = 1;
    int caidan_flag = 0;
    int tu_flag = 0;
    int tu_model = 0;
	int tuichu = 0;
    //打开触摸
	ts_open();
    //显示初始界面
    show_bmp("./pic/1.bmp",0,0);
	while(1)
	{
		// printf("点击获取坐标\n");
        //实时获取坐标信息
		ts_get(&x,&y);
		printf("(%d,%d)\n",x,y);
        // printf("caidan_flag = %d,%d\n",x,y);
        //进度条
		if(jindu_flag){
            if(x >= 450 && x <= 600 && y >= 240 && y <= 300){
                jindu();
                jindu_flag = 0;
                caidan_flag = 1;
            }
        }
        //进入相册菜单
        if(jindu_flag == 0 && caidan_flag == 1){
            show_bmp("./pic/5.bmp",0,0);
        }

        //点击打开相册
        if(caidan_flag){
            if(x >= 547 && x <= 770 && y >= 313 && y <= 400){
                caidan_flag = 0;
				tu_flag = 1;
            }
        }

        //图片切换
        if(tu_flag){
            //返回
            if(x >= 267 && x <= 533){
                tuichu = 1;
            }
            //下一张
            if(x >= 534 && x <= 799){
                if(tu_model == 4){
                    tu_model = 0;
                }else{
                    tu_model++;
                }
            }
            //上一张
            if(x >= 0 && x <= 266){
                if(tu_model == 0){
                    tu_model = 4;
                }else{
                    tu_model--;
                }
            }
            //图片显示
            switch(tu_model){
                case 0:
                show_bmp("./pic/2.bmp",0,0);
                break;
                case 1:
                show_bmp("./pic/3.bmp",0,0);
                break;
                case 2:
                show_bmp("./pic/4.bmp",0,0);
                break;
                case 3:
                show_bmp("./pic/6.bmp",0,0);
                break;
				case 4:
				{
					show_bmp("./pic/10.bmp",0,0);
					show_bmp("./pic/7.bmp",200,100);
					break;
				}
            }
        }
		
		if(tuichu){
			show_bmp("./pic/8.bmp",0,0);
			break;
		}
	}
	close(ts_fd);
	return 0;
}
